package org.adream.account.rest.api;

import javax.servlet.http.HttpServletRequest;

import org.adream.account.entity.RealNameTmpEntity;
import org.adream.account.entity.UserEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.service.BasicInfoService;
import org.adream.account.service.RealNameTmpService;
import org.adream.account.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/basicInfo")
@Controller
public class BasicInfoApiController {
	
	private final static Logger logger = LoggerFactory.getLogger(BasicInfoApiController.class);
	
	@Autowired
	private BasicInfoService basicService;
	
	@Autowired
	private RealNameTmpService realNameTmpService;
	
	/**
	 * 查询用户基本信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<UserEntity> queryBasicInfo(HttpServletRequest request){
		String uid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(uid)) {
			return new ResultModel<UserEntity>(ResultModel.ERR_PARAM, "用户信息uid为空", null);
		}
		UserEntity user = basicService.queryBasicInfo(uid);
		if(user == null) {
			return new ResultModel<UserEntity>(ResultModel.NOT_FOUND_OBJECT, "用户不存在", null);
		}
		return new ResultModel<UserEntity>(ResultModel.SUCCESS, "查询成功", user);
	}
	
	/**
	 * 查询用户的真实姓名和手机号
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getNameAndPhone", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> getNameAndPhone(HttpServletRequest request){
		String uid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(uid)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "用户信息uid为空", null);
		}
		UserEntity user = basicService.queryBasicInfo(uid);
		if(user != null && StringUtils.isEmpty(user.getRealName())) {
			RealNameTmpEntity realNameTmpEntity = realNameTmpService.queryTmpByUid(uid);
			if(realNameTmpEntity != null) {
				user.setRealName(realNameTmpEntity.getRealName());
			}
		}
		return new ResultModel<UserEntity>(ResultModel.SUCCESS, "查询成功", user);
	}
}